package com.jeesite.modules.wzt.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.dao.EmployeeOfficeDao;
import com.jeesite.modules.sys.entity.EmployeeOffice;
import com.jeesite.modules.wzt.dao.WztOrgDao;
import com.jeesite.modules.wzt.entity.WztOrg;
import com.jeesite.modules.wzt.entity.WztUser;
import com.jeesite.modules.wzt.util.BamboocloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wzt.entity.WztAsg;
import com.jeesite.modules.wzt.dao.WztAsgDao;

/**
 * 任职属性Service
 * @author xhc
 * @version 2023-03-30
 */
@Service
@Transactional(readOnly=false)
public class WztAsgService extends CrudService<WztAsgDao, WztAsg> {
	@Autowired
	private WztUserService wztUserService;
	@Autowired
	private WztOrgDao wztOrgDao;
	@Autowired
	private EmployeeOfficeDao employeeOfficeDao;
	/**
	 * 获取单条数据
	 * @param wztAsg
	 * @return
	 */
	@Override
	public WztAsg get(WztAsg wztAsg) {
		return super.get(wztAsg);
	}
	
	/**
	 * 查询分页数据
	 * @param wztAsg 查询条件
	 * @param wztAsg.page 分页对象
	 * @return
	 */
	@Override
	public Page<WztAsg> findPage(WztAsg wztAsg) {
		return super.findPage(wztAsg);
	}
	
	/**
	 * 查询列表数据
	 * @param wztAsg
	 * @return
	 */
	@Override
	public List<WztAsg> findList(WztAsg wztAsg) {
		return super.findList(wztAsg);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param wztAsg
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(WztAsg wztAsg) {
		super.save(wztAsg);
	}
	
	/**
	 * 更新状态
	 * @param wztAsg
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(WztAsg wztAsg) {
		super.updateStatus(wztAsg);
	}
	
	/**
	 * 删除数据
	 * @param wztAsg
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(WztAsg wztAsg) {
		super.delete(wztAsg);
	}
	@Transactional(readOnly=false)
	public Map<String, Object> creatAsg(Map<String, Object> reqmap) {
		Map<String,Object> resMap = new HashMap<>();
		if (checkAsgParam(reqmap, resMap)) return resMap;
		WztAsg wztAsg  = new WztAsg();
		try {
			BamboocloudUtils.copyProperties(reqmap,wztAsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WztUser wztUser = wztUserService.get(wztAsg.getUserId());
		if(!StringUtils.equals(wztUser.getOrganizitionId(),wztAsg.getOrganizationId())){
			WztOrg wztOrg = wztOrgDao.queryOfficeByWztOrgTable(wztAsg.getOrganizationId());
			if(wztOrg==null){
				resMap.put("resultCode","500");
				resMap.put("message","机构:"+wztAsg.getOrganizationId()+"不存在请先创建此机构");
				return resMap;
			}
			EmployeeOffice employeeOffice = new EmployeeOffice();
			employeeOffice.setId(IdGen.nextId());
			employeeOffice.setEmpCode(wztUser.getSysUid());
			employeeOffice.setOfficeCode(wztOrg.getOfficeCode());
			EmployeeOffice employeeOffice_db  = dao.findEmployeeOffice(wztUser.getSysUid(),wztOrg.getOfficeCode());
			if(employeeOffice_db==null){
				employeeOfficeDao.insert(employeeOffice);
			}

		}
		dao.insert(wztAsg);
		return resMap;
	}

	private boolean checkAsgParam(Map<String, Object> reqmap, Map<String, Object> resMap) {
		if(reqmap.get("userId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","userId必传");
			return true;
		}
		if(reqmap.get("organizationId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","organizationId必传");
			return true;
		}
		return false;
	}
	@Transactional(readOnly=false)
	public Map<String, Object> updateAsg(Map<String, Object> reqmap) {
		Map<String,Object> resMap = new HashMap<>();
		if(reqmap.get("userId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","userId必传");
			return resMap;
		}
		if(reqmap.get("organizationId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","organizationId必传");
			return resMap;
		}
		WztAsg wztAsg  = new WztAsg();
		try {
			BamboocloudUtils.copyProperties(reqmap,wztAsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WztUser wztUser = wztUserService.get(wztAsg.getUserId());

		if(StringUtils.equals(wztUser.getOrganizitionId(),wztAsg.getOrganizationId())){
			WztOrg wztOrg = new WztOrg();
			wztOrg.setCode(wztAsg.getOrganizationId());
			WztOrg wztOrg_db = wztOrgDao.get(wztOrg);
			long res = dao.updateEmployeeOffice(wztUser.getSysUid(),wztOrg_db.getOfficeCode(),wztOrg_db.getOfficeName());
			if(res!=1){
				resMap.put("resultCode","500");
				resMap.put("message","更新任职属性失败");
				return resMap;
			}
		}
		if(StringUtils.equals(wztAsg.getIsDelete(),"1")){
			dao.delete(wztAsg);
			WztOrg wztOrg = new WztOrg();
			wztOrg.setCode(wztAsg.getOrganizationId());
			WztOrg wztOrg_db = wztOrgDao.get(wztOrg);
			dao.deleteEmployeeOfficeByOrgId(wztUser.getSysUid(),wztOrg_db.getOfficeCode());
			return resMap;
		}
		dao.update(wztAsg);
		return resMap;
	}

	public Map<String, Object> queryAsg(Map<String, Object> reqmap) {
		Map<String,Object> resMap = new HashMap<>();
		if(reqmap.get("userId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","userId必传");
			return resMap;
		}
		WztAsg wztAsg  = new WztAsg();
		wztAsg.setUserId(reqmap.get("userId").toString());
		List<WztAsg> list = dao.findList(wztAsg);
//		if(list==null || list.size()==0 ){
//			WztUser wztUser = wztUserService.get(wztAsg.getUserId());
//			long res =dao.deleteEmployeeOffice(wztUser.getSysUid());
//		}
		resMap.put("assignment",list);
		return resMap;
	}
}